<?php

namespace App\Services;
use App\Model\Contract;
use App\Model\LocalContract;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;

class QiniuService
{
    public $accessKey = 'ja9K3G7K4gTc0Fa0OVt-XXJvkE2HuCfp2bTVf-R7';
    public $secretKey = 'ixpGS5EN3fhK3xK2QPTKA2wvB74w3lQwKpA3A0a9';
    public $bucket = 'mihetong';

    public function upload($contracts){
// 构建鉴权对象
//        LocalContract::where('id',$contracts['id'])->update(['is_make'=>2]);
        $auth = new Auth($this->accessKey, $this->secretKey);
// 生成上传 Token
        $token = $auth->uploadToken($this->bucket);
        $filePath = BASE_PATH .'/public/'.$contracts['path'];
        $path = explode('/',$contracts['path']);
         $key = 'doc/'.date('Ymd').'/'.end($path);
         $imgs = json_decode($contracts['png']);
        // 初始化 UploadManager 对象并进行文件的上传。
        $uploadMgr = new UploadManager();
        // 调用 UploadManager 的 putFile 方法进行文件的上传。
        if(file_exists($filePath)){
            list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath, null, 'application/octet-stream', true, null, 'v2');
            if ($err !== null) {
                $data = [];
            } else {
//            unlink($filePath);
                $data['path'] = $ret['key'];
                $data['title'] = $contracts['title'];
                $data['class_id'] =$contracts['class_id'];
                $data['size'] =$contracts['size'];
                $data['download'] = $contracts['download'];
                $data['ext'] = $contracts['ext'];
                $data['png_count'] = $contracts['png_count'];
                $data['tid'] =$ret['hash'];
                $newImgs = [];
                if(!file_exists(BASE_PATH .'/public/'.$imgs[0])){
                    LocalContract::where('id',$contracts['id'])->update(['is_make'=>4]);
                    return ;
                }
                $thumb = self::makeWater(BASE_PATH .'/public/'.$imgs[0],$contracts['title']);
                $filePath = BASE_PATH .'/public/'.$thumb;
                $path = explode('/',$thumb);
                $key = 'thumb/'.date('Ymd').'/'.end( $path);
                list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath, null, 'application/octet-stream', true, null, 'v2');
                if ($err !== null) {
                    $data['title_pic'] ='失败';
                }else{
                    $data['title_pic'] = $ret['key'];
                }
                unlink($filePath);
                foreach($imgs as $k => $v){
                    $filePath = BASE_PATH .'/public/'.$v;
                    $path = explode('/',$v);
                    $key = 'img/'.date('Ymd').'/'.end( $path);
                    if(!file_exists($filePath)){
                        LocalContract::where('id',$contracts['id'])->update(['is_make'=>4]);
                        return ;
                    }
                    list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath, null, 'application/octet-stream', true, null, 'v2');
                    if ($err !== null) {

                    }else{
//                    unlink($filePath);
                      $newImgs[] = $ret['key'];
                    }
                }
                $data['png'] = json_encode($newImgs);
                $data['is_finish'] = 1;
            }
            if($data){
                Contract::create($data);
            }
        }else{
            LocalContract::where('id',$contracts['id'])->update(['is_make'=>4]);
            return ;
        }
    }

    /**生成水印
     * @param $params
     * @return void
     */
    public static function makeWater($url,$title){
        $backgroud =  BASE_PATH .'/public/static/test.png';
        $handler =  \think\Image::open($backgroud);
        $uuid = uniqid();
        $date = date('Y-m-d',time());
        $local = [25, 65];
        $thumb =  \think\Image::open($url);
        $thumb->thumb(230, 590);
        if(!is_dir( BASE_PATH .'/public/upload/thumb/')){
            mkdir( BASE_PATH .'/public/upload/thumb/',0777,true);
        }
        $thumb->save(BASE_PATH .'/public/upload/thumb/'.$uuid.'.jpg');
        $handler->text($title,  BASE_PATH .'/public/static/song.ttf', 13,'#464646',[68,23]);
        $handler->text('word格式/A4打印/内容随意修改', BASE_PATH .'/public/static/song.ttf', 7,'#00000000',[68,48]);
        $handler->water(BASE_PATH .'/public/upload/thumb/'.$uuid.'.jpg', $local); // $path 图片本地路径  $local 数组 [$x, $y];
        $handler->save(BASE_PATH .'/public/upload/thumb/'.$uuid.'.jpg');
        return '/upload/thumb/'.$uuid.'.jpg';
    }

}
